import { observable, action } from 'mobx';
import { transManage } from '../fetch/apisetting';

class TransManage {
    @observable inProgress = false;
    @observable userList = [];
    @observable userListAll = [];
    @observable productList = [];
    @observable productListAll = [];
    @observable productsubList = [];
    @observable visible = false;
    @observable productListTable = [];
    @observable title = '';
    @observable addobj={
        code:'',
        name:'',
        phone:'',
        developerid:localStorage.getItem('userData')?JSON.parse(localStorage.getItem('userData')).id:null
    }
    @observable DeveloperUserProductList={
        developerUserId:0,
        month:new Date().getFullYear()+'-'+(new Date().getMonth()+1),
    }
    @observable DeveloperUserProductUseDetail={
        productId:0,
        month:new Date().getFullYear()+'-'+(new Date().getMonth()+1),
        pagesize:10,
        pagenow:1
    }
    @observable tableData = [];
    @observable paginationProduct={
        defaultCurrent:1,
        defaultPageSize:10,
        current:1,
        total:0
    }
    @observable pagination={
        defaultCurrent:1,
        defaultPageSize:10,
        current:1,
        total:0
    }
    @observable param={
        "developerid":localStorage.getItem('userData')?JSON.parse(localStorage.getItem('userData')).id:null,
        "pagesize":"10",
        "pagenow":this.pagination.current
    }
    @observable developerid=localStorage.getItem('userData')?JSON.parse(localStorage.getItem('userData')).id:null
    @action getUserList(){
        // this.inProgress=true
        return transManage.getUserList().then(
            action('getUserList',res=>{
                window.success(res.errormsg)
                this.inProgress = false
                console.log(res.data.length)
                if(res.data.length) {
                        this.userListAll = this.userList = res.data
                        this.DeveloperUserProductList.developerUserId = res.data[0].developerid
                        this.getDeveloperUserProductList()
                    }
        })
        )
    }
    @action getProductList(){
    this.inProgress=true
    return transManage.getSubsProductList({id:this.developerid,type:0}).then(
        action('getProductList',res=>{
            window.success(res.errormsg)
        this.productListAll=res.data;
        this.inProgress = false;
         if(res.data.length){

             this.title=res.data[0].name
             this.DeveloperUserProductUseDetail.productId=res.data[0].id
             this.getDeveloperUserProductUseDetail()
             this.productList=res.data;

         }

})
)
}
    @action getGusseData(){
        return transManage.getGusseData(this.param).then(
            action('getGusseData',res=>{
                // window.success(res.errormsg)
                this.tableData=res.data.list;
                this.pagination.total=res.data.totalCount;
                this.inProgress = false;
            })
        )
    }
    @action addUser(){
        return transManage.addUser(this.addobj).then(
            action('addUser',res=>{
                window.success(res.errormsg)
                this.getGusseData()
            })
        )
    }
    @action deleteUser(data={}){
        return transManage.deleteUser(data).then(
            action('deleteUser',res=>{
                window.success(res.errormsg)
                this.getGusseData()
            })
        )
    }
    @action getDeveloperUserProductList(){
        return transManage.getDeveloperUserProductList(this.DeveloperUserProductList).then(
            action('getDeveloperUserProductList',res=>{
                window.success(res.errormsg)
                this.productsubList=res.data
            })
        )
    }
    @action getDeveloperUserProductUseDetail(){
        return transManage.getDeveloperUserProductUseDetail(this.DeveloperUserProductUseDetail).then(
            action('getDeveloperUserProductUseDetail',res=>{
                window.success(res.errormsg)
                this.productListTable=res.data.list
                this.paginationProduct.total=res.data.totalCount
            })
        )
    }
}

export default new TransManage();
